<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-flat-top.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":-1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="今天练习的程序是实现智能小夜灯的控制，首先，通过太极创客的视频来看看最终的实现结果：     本次程序例程来自太极创客官网，此平台的Arduino教程深入浅出，对于想学习Arduino的同学，首推太极创客团队。">
<meta property="og:type" content="article">
<meta property="og:title" content="🚗造轮子—智能小夜灯">
<meta property="og:url" content="http://example.com/2022/03/09/%E9%80%A0%E8%BD%AE%E5%AD%90%E2%80%94%E6%99%BA%E8%83%BD%E5%B0%8F%E5%A4%9C%E7%81%AF/index.html">
<meta property="og:site_name" content="Redamancy785&#39;s Blog">
<meta property="og:description" content="今天练习的程序是实现智能小夜灯的控制，首先，通过太极创客的视频来看看最终的实现结果：     本次程序例程来自太极创客官网，此平台的Arduino教程深入浅出，对于想学习Arduino的同学，首推太极创客团队。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-03-09T05:30:18.000Z">
<meta property="article:modified_time" content="2022-03-09T07:24:50.107Z">
<meta property="article:author" content="Redamancy785">
<meta property="article:tag" content="Arduino">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2022/03/09/%E9%80%A0%E8%BD%AE%E5%AD%90%E2%80%94%E6%99%BA%E8%83%BD%E5%B0%8F%E5%A4%9C%E7%81%AF/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>🚗造轮子—智能小夜灯 | Redamancy785's Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>


    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Redamancy785's Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">在不动声色的日子里,遇见你真好</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
	  <a target="_blank" rel="noopener" href="https://github.com/happymrli" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/03/09/%E9%80%A0%E8%BD%AE%E5%AD%90%E2%80%94%E6%99%BA%E8%83%BD%E5%B0%8F%E5%A4%9C%E7%81%AF/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://sf3-cdn-tos.toutiaostatic.com/img/user-avatar/32da60bc0440a8f7022acf22d74df3ac~300x300.image">
      <meta itemprop="name" content="Redamancy785">
      <meta itemprop="description" content="野生钢铁侠">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Redamancy785's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          🚗造轮子—智能小夜灯
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2022-03-09 13:30:18 / 修改时间：15:24:50" itemprop="dateCreated datePublished" datetime="2022-03-09T13:30:18+08:00">2022-03-09</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%AD%A6%E4%B9%A0/" itemprop="url" rel="index"><span itemprop="name">学习</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2022/03/09/%E9%80%A0%E8%BD%AE%E5%AD%90%E2%80%94%E6%99%BA%E8%83%BD%E5%B0%8F%E5%A4%9C%E7%81%AF/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2022/03/09/%E9%80%A0%E8%BD%AE%E5%AD%90%E2%80%94%E6%99%BA%E8%83%BD%E5%B0%8F%E5%A4%9C%E7%81%AF/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>4.7k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>4 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>今天练习的程序是实现<code>智能小夜灯</code>的控制，首先，通过<code>太极创客</code>的视频来看看最终的实现结果：</p>
<iframe src="//player.bilibili.com/player.html?aid=52752625&bvid=BV1b441177VF&cid=92305535&page=9" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"> </iframe>

<blockquote>
<p>本次程序例程来自<a target="_blank" rel="noopener" href="http://www.taichi-maker.com/">太极创客官网</a>，此平台的Arduino教程深入浅出，对于想学习Arduino的同学，首推太极创客团队。</p>
</blockquote>
<span id="more"></span>

<p>以下便是今天练习的造轮子程序：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> *基本功能介绍：</span></span><br><span class="line"><span class="comment"> * 用户可通过传感器控制RGB-LED点亮和关闭</span></span><br><span class="line"><span class="comment"> * 小夜灯配备人体红外感应传感器以及光敏电阻实现照明自动化</span></span><br><span class="line"><span class="comment"> * 点亮和熄灭时产生渐明渐暗效果</span></span><br><span class="line"><span class="comment"> * 点亮时色彩不断变化</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 基本电路连接说明：</span></span><br><span class="line"><span class="comment"> * 共阴极RGB-LED引脚R 连接 Arduino控制器引脚6</span></span><br><span class="line"><span class="comment"> * 共阴极RGB-LED引脚G 连接 Arduino控制器引脚5</span></span><br><span class="line"><span class="comment"> * 共阴极RGB-LED引脚B 连接 Arduino控制器引脚3</span></span><br><span class="line"><span class="comment"> * 红外人体感应模块信号输出引脚 连接 Arduino控制器引脚8</span></span><br><span class="line"><span class="comment"> * 光敏电阻分压电路信号输出引脚 连接 Arduino控制器引脚A0</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//宏定义引脚</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> rLedPin 6</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> gLedPin 5</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> bLedPin 3</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> irSensorPin 8</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> lightSensorPin A0</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//led亮度</span></span><br><span class="line"><span class="type">int</span> ledR=<span class="number">0</span>;</span><br><span class="line"><span class="type">int</span> ledG=<span class="number">0</span>;</span><br><span class="line"><span class="type">int</span> ledB=<span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> irReading; <span class="comment">//红外人体感应模块输出</span></span><br><span class="line"><span class="type">int</span> lightRedaing; <span class="comment">//光敏电阻分压电路信号输出</span></span><br><span class="line"><span class="type">bool</span> onOffState; <span class="comment">//小夜灯开关状态</span></span><br><span class="line"></span><br><span class="line"><span class="type">unsigned</span> <span class="type">long</span> previousIrMillis; <span class="comment">//上一次检查红外传感器的时间</span></span><br><span class="line"><span class="type">unsigned</span> <span class="type">long</span> previousLightMillis; <span class="comment">//上一次检查光敏传感器的时间</span></span><br><span class="line"><span class="type">int</span> irCheckInterval=<span class="number">500</span>; <span class="comment">//红外传感器检查时间间隔</span></span><br><span class="line"><span class="type">int</span> irCheckInterval=<span class="number">1000</span>; <span class="comment">//光敏传感器检查时间间隔</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> colorIndex; <span class="comment">//颜色序列号</span></span><br><span class="line"><span class="type">int</span> colorChangeDelay=<span class="number">1</span>; <span class="comment">//颜色改变速度控制变量</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">setup</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">//设置引脚工作模式</span></span><br><span class="line">    <span class="built_in">pinMode</span>(rLedPin,OUTPUT);</span><br><span class="line">    <span class="built_in">pinMode</span>(gLedPin,OUTPUT);</span><br><span class="line">    <span class="built_in">pinMode</span>(bLedPin,OUTPUT);</span><br><span class="line">    <span class="built_in">pinMode</span>(irSensorPin,INPUT);</span><br><span class="line">    </span><br><span class="line">    Serial.<span class="built_in">begin</span>(<span class="number">9600</span>);</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;Welcome!&quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;===System Start Sensor Cheak===&quot;</span>);</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//读取红外人体感应模块</span></span><br><span class="line">    irReading=<span class="built_in">digitalRead</span>(irSensorPin);</span><br><span class="line">    lightRedaing=<span class="built_in">anologRead</span>(lightSensorPin);</span><br><span class="line">    </span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;===Checking Sensor===&quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">print</span>(<span class="string">&quot;irReading&quot;</span>);Serial.<span class="built_in">println</span>(irReading);</span><br><span class="line">    Serial.<span class="built_in">print</span>(<span class="string">&quot;lightRedaing&quot;</span>);Serial.<span class="built_in">println</span>(lightRedaing);</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;======================&quot;</span>);</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//六个函数  </span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">loop</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="type">unsigned</span> <span class="type">long</span> currentMillis=<span class="built_in">millis</span>();<span class="comment">//millis()函数可以用来获取Arduino开机后的运行时间</span></span><br><span class="line">    </span><br><span class="line">    <span class="built_in">irCheak</span>(currentMillis);<span class="comment">//检查红外传感器时间</span></span><br><span class="line">    <span class="built_in">lightCheak</span>(currentMillis);<span class="comment">//检查光敏电阻时间</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span>(irReading==HIGH)&#123;</span><br><span class="line">        irCheckInterval=<span class="number">30000</span>;</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        irCheckInterval=<span class="number">500</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span>(irReading==HIGH&amp;&amp;lightRedaing&gt;=<span class="number">880</span>)&#123;</span><br><span class="line">        <span class="keyword">if</span>(onOffState==<span class="number">0</span>)</span><br><span class="line">            <span class="built_in">fadeOn</span>();</span><br><span class="line">        onOffState=<span class="number">1</span>;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">if</span>(colorIndex&lt;=<span class="number">1535</span>)&#123;</span><br><span class="line">            colorIndex++；</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(colorIndex&gt;<span class="number">1535</span>)&#123;</span><br><span class="line">            colorIndex=<span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">ledShowColor</span>(colorIndex)</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(onOffState==<span class="number">1</span>)</span><br><span class="line">            <span class="built_in">fadeOff</span>();</span><br><span class="line">        onOffState=<span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">irCheak</span><span class="params">(<span class="type">unsigned</span> <span class="type">long</span> currentMillis)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(currentMillis-previousIrMillis)&gt;=irCheakInterval)&#123;</span><br><span class="line">        irReading=<span class="built_in">digitalRead</span>(irSensorPin);</span><br><span class="line"></span><br><span class="line">    	Serial.<span class="built_in">println</span>(<span class="string">&quot;===Checking IR Sensor===&quot;</span>);</span><br><span class="line">    	Serial.<span class="built_in">print</span>(<span class="string">&quot;irReading=&quot;</span>);Serial.<span class="built_in">println</span>(irReading);</span><br><span class="line">    	Serial.<span class="built_in">print</span>(<span class="string">&quot;currentMillis&quot;</span>);Serial.<span class="built_in">println</span>(currentMillis);</span><br><span class="line">       	Serial.<span class="built_in">print</span>(<span class="string">&quot;previousIrMillis&quot;</span>);Serial.<span class="built_in">println</span>(previousIrMillis);</span><br><span class="line">        Serial.<span class="built_in">println</span>(<span class="string">&quot;=========================&quot;</span>);</span><br><span class="line">        previousIrMillis=currentMillis;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">lightCheak</span><span class="params">(<span class="type">unsigned</span> <span class="type">long</span> currentMillis)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(currentMillis-previousLightMillis)&gt;=lightCheakInterval)&#123;</span><br><span class="line">		lightRedaing=<span class="built_in">anologRead</span>(lightSensorPin);</span><br><span class="line">        </span><br><span class="line">    	Serial.<span class="built_in">println</span>(<span class="string">&quot;===Checking Light Sensor===&quot;</span>);</span><br><span class="line">    	Serial.<span class="built_in">print</span>(<span class="string">&quot;lightReading=&quot;</span>);Serial.<span class="built_in">println</span>(lightRedaing);</span><br><span class="line">    	Serial.<span class="built_in">print</span>(<span class="string">&quot;currentMillis&quot;</span>);Serial.<span class="built_in">println</span>(currentMillis);</span><br><span class="line">        Serial.<span class="built_in">print</span>(<span class="string">&quot;previousLightMillis&quot;</span>);Serial.<span class="built_in">println</span>(previousLightMillis);</span><br><span class="line">        Serial.<span class="built_in">println</span>(<span class="string">&quot;===========================&quot;</span>);</span><br><span class="line">        </span><br><span class="line">        previousLightMillis=currentMillis;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">ledShowColor</span><span class="params">(<span class="type">int</span> colorIndex)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(colorIndex&gt;=<span class="number">0</span>&amp;&amp;colorIndex&lt;=<span class="number">255</span>)&#123;</span><br><span class="line">        ledR=<span class="number">255</span>-colorIndex;</span><br><span class="line">        <span class="built_in">anologWrite</span>(rLedPin,ledR);</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(colorIndex&gt;=<span class="number">256</span>&amp;&amp;colorIndex&lt;=<span class="number">511</span>)&#123;</span><br><span class="line">        ledR=colorIndex<span class="number">-256</span>;</span><br><span class="line">        <span class="built_in">anologWrite</span>(rLedPin,ledR);</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(colorIndex&gt;=<span class="number">512</span>&amp;&amp;colorIndex&lt;=<span class="number">767</span>)&#123;</span><br><span class="line">        ledG=<span class="number">767</span>-colorIndex;</span><br><span class="line">        <span class="built_in">anologWrite</span>(gLedPin,ledG);</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(colorIndex&gt;=<span class="number">768</span>&amp;&amp;colorIndex&lt;=<span class="number">1023</span>)&#123;</span><br><span class="line">        ledG=colorIndex<span class="number">-768</span>;</span><br><span class="line">        <span class="built_in">anologWrite</span>(gLedPin,ledG);</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(colorIndex&gt;=<span class="number">1024</span>&amp;&amp;colorIndex&lt;=<span class="number">1279</span>)&#123;</span><br><span class="line">        ledB=<span class="number">1279</span>-colorIndex;</span><br><span class="line">        <span class="built_in">anologWrite</span>(bLedPin,ledB);</span><br><span class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(colorIndex&gt;=<span class="number">1280</span>&amp;&amp;colorIndex&lt;=<span class="number">1535</span>)&#123;</span><br><span class="line">        ledB=colorIndex<span class="number">-1280</span>;</span><br><span class="line">        <span class="built_in">anologWrite</span>(bLedPin,ledB);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">delay</span>(colorChangeDelay);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">fadeOn</span><span class="params">()</span></span>&#123;</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;Fade On&quot;</span>);</span><br><span class="line">    <span class="type">int</span> i;</span><br><span class="line">    <span class="keyword">while</span>(i&lt;<span class="number">255</span>)&#123;</span><br><span class="line">        i++;</span><br><span class="line">        ledR++;</span><br><span class="line">        ledG++;</span><br><span class="line">        ledB++;</span><br><span class="line">        <span class="built_in">anologWrite</span>(rLedPin,ledR);</span><br><span class="line">        <span class="built_in">anologWrite</span>(gLedPin,ledG);</span><br><span class="line">        <span class="built_in">anologWrite</span>(bLedPin,ledB);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">fadeOff</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="keyword">while</span>(ledR&gt;<span class="number">0</span>)&#123;</span><br><span class="line">        ledR--;</span><br><span class="line">        <span class="built_in">anologWrite</span>(rLedPin,ledR);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(ledG&gt;<span class="number">0</span>)&#123;</span><br><span class="line">        ledG--;</span><br><span class="line">        <span class="built_in">anologWrite</span>(gLedPin,ledG);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(ledB&gt;<span class="number">0</span>)&#123;</span><br><span class="line">        ledB--;</span><br><span class="line">        <span class="built_in">anologWrite</span>(bLedPin,ledB);</span><br><span class="line">    &#125;    </span><br><span class="line">    colorIndex=<span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>心得：</p>
<p>变量的声明其实可以更加详细一点，可以使用<code>完整的英文</code>来表示，而不是只使用单词的开头字母。因为在真正写代码的过程中，变量其实大多不是自己拼出来，而是直接复制粘贴，而在此时，准确知道变量的含义并复制来使用尤为重要。所以<code>不要忌讳变量名过长</code>，<code>能快速识别并且能拿来使用是更重要的</code>。</p>
</blockquote>

    </div>

    
    
    
        <div class="reward-container">
  <div>我不管，我要吃棒棒糖~</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/weixin.jpg" alt="Redamancy785 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/zhifubao.jpg" alt="Redamancy785 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/Arduino/" rel="tag"><i class="fa fa-tag"></i> Arduino</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/03/09/%E4%BA%BA%E7%94%9F%E7%9A%84%E5%85%B3%E9%94%AE%E8%AF%8D/" rel="prev" title="人生的关键词">
      <i class="fa fa-chevron-left"></i> 人生的关键词
    </a></div>
      <div class="post-nav-item">
    <a href="/2022/03/09/%E9%80%A0%E8%BD%AE%E5%AD%90%E2%80%94Flash%E4%BC%98%E5%8C%96%E4%BB%A3%E7%A0%81/" rel="next" title="🚗造轮子—Flash优化">
      🚗造轮子—Flash优化 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Redamancy785"
      src="https://sf3-cdn-tos.toutiaostatic.com/img/user-avatar/32da60bc0440a8f7022acf22d74df3ac~300x300.image">
  <p class="site-author-name" itemprop="name">Redamancy785</p>
  <div class="site-description" itemprop="description">野生钢铁侠</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">9</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/happymrli" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;happymrli" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/u/7464024291" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;u&#x2F;7464024291" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://space.bilibili.com/629004933" title="https:&#x2F;&#x2F;space.bilibili.com&#x2F;629004933" rel="noopener" target="_blank">Bilibili</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://me.csdn.net/weixin_51460407" title="https:&#x2F;&#x2F;me.csdn.net&#x2F;weixin_51460407" rel="noopener" target="_blank">CSDN</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.toutiao.com/c/user/token/MS4wLjABAAAATetYsKx5ltYrGCleC17q6YEveJEd18RvhE4Qpylwm9KbjXThfi_M337_48X95DBn" title="https:&#x2F;&#x2F;www.toutiao.com&#x2F;c&#x2F;user&#x2F;token&#x2F;MS4wLjABAAAATetYsKx5ltYrGCleC17q6YEveJEd18RvhE4Qpylwm9KbjXThfi_M337_48X95DBn" rel="noopener" target="_blank">今日头条</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://mrli123456789.github.io/" title="https:&#x2F;&#x2F;mrli123456789.github.io&#x2F;" rel="noopener" target="_blank">野生钢铁侠の知识库</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-[object Object]"></i>
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Redamancy785</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">14k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">13 分钟</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/canvas-ribbon/canvas-ribbon.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'a2cwDaNdJ6fps0LaWYNPVQKA-gzGzoHsz',
      appKey     : 'hiUU3Wae5NCzTgAwMYGuk0b1',
      placeholder: "发一条友善的评论",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
